from . import admin
from flask import render_template,jsonify,request
from flask_login import login_required
from models.game import GameGame,UserGame
from models.user import UserUser
from models.payment import PaymentPay
import random
default_open = ['2']

@admin.route('/recommend')
@login_required
def recommend():
    user = UserUser.query.get(1)
    same_user = UserUser.query.filter_by(like_game_tag=user.like_game_tag).filter(UserUser.id!=user.id).all()
    if len(same_user)>3:
        same_user = random.sample(same_user,3)
    recommend_game = user.recommend_game()
    return render_template('recommend/user_recommend.html',
                           default_open=default_open,user=user,same_user=same_user,recommend_game=recommend_game)

@admin.route('/user_center')
@login_required
def user_center():
    user_id = request.args.get('user_id',1)
    user_name = request.args.get('user_name','')
    user = UserUser.query
    id = ''
    login_name = ''
    if user_id:
        id = user_id
        user = user.filter_by(id=user_id)
    if user_name:
        login_name = user_name
        user = user.filter_by(name=user_name)
    user = user.first()
    game_play_list = []
    charge_list = []
    if user:
        game_play_list = UserGame.query.filter_by(user_id=user.id).order_by(UserGame.id.desc()).limit(5).all()
        charge_list = PaymentPay.query.filter_by(user_id=user.id,order_status=1).order_by(PaymentPay.order_time.desc()).limit(10).all()
    return render_template('recommend/recommend.html',default_open=default_open,id=id,login_name=login_name,user=user,
                           game_play_list=game_play_list,charge_list=charge_list)